package cn.itcast.reggie.controller;

import cn.itcast.reggie.common.R;
import cn.itcast.reggie.pojo.Employee;
import cn.itcast.reggie.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Date;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R login(@RequestBody Employee employee, HttpSession session) {
        //1.接受前端传递用户名和密码
        //2.对前端传递的明文密码进行加密
        String password = employee.getPassword();
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        //3.根据用户名查询employee对象
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",employee.getUsername());
        //select * from employee where username = ?
        Employee existsEmployee = employeeService.getOne(queryWrapper);
        if(existsEmployee == null) {
            //用户名错误
            return R.error("用户名不存在");
        }

        //4.校验密码,数据库密码 和 前端传递明文密码加密结果
        if(!existsEmployee.getPassword().equals(md5Password)) {
            return R.error("密码错误");
        }

        //5.密码正确,判断是否账户被禁用
        if(existsEmployee.getStatus() == 0) {
            return R.error("账户被禁用，暂时不能使用");
        }

        //6.登录成功

        session.setAttribute("employee",existsEmployee.getId());
        return R.success(existsEmployee);

    }

    @PostMapping("/logout")
    public R logout(HttpSession session) {
        session.removeAttribute("employee");
        return R.success("退出成功");
    }


    @GetMapping("/page")
    public R findByPage(Integer page,Integer pageSize,String name) {

        IPage<Employee> p = new Page<>(page,pageSize);
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(name)) {
            queryWrapper.like("name",name);
        }
        //result中包含了分页的所有信息(当前页，每页显示条数，总记录数，当前页数据，总页数..)
        IPage<Employee> result = employeeService.page(p, queryWrapper);
        return R.success(result);
    }
    @PostMapping
    public R addEmployee(@RequestBody Employee employee,HttpSession session) {
        //2.补齐属性
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

    /*    employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        employee.setCreateUser((Long) session.getAttribute("employee"));
        employee.setUpdateUser((Long) session.getAttribute("employee"));*/

        employeeService.save(employee);
        return R.success("添加成功");
    }
    @PutMapping
    public R update(@RequestBody Employee employee,HttpSession session) {
        Long employeeId = (Long) session.getAttribute("employee");
        if(employeeId != 1L) {
            return R.error("非法操作");
        }

        employee.setStatus(employee.getStatus());

      /*  employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(employeeId);*/

        employeeService.updateById(employee);
        return R.success("修改成功！");
    }

    @GetMapping("/{id}")
    public R findById(@PathVariable("id") Long id) {
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }


}
